<?php
/**
 * Created by PhpStorm.
 * User: PHP
 * Date: 2021/6/7
 * Time: 17:35
 */
namespace App\Http\Admin\Controllers\Notice;
use App\Http\Controllers\Controller;
use Hlcreation\Basic\Helper;
use App\Services\notice\NoticeServices;
use Illuminate\Http\Request;
use App\Requests\NoticeRequest;
use Illuminate\Support\Facades\DB;
use App\Services\store\StoreServices;

class PlacardController extends Controller
{

    public function __construct(NoticeServices $services)
    {
        $this->services = $services;
    }



    //公告列表11111
    public function index(Request $request,NoticeServices $noticeServices){

        $data = $request->only('page','limit','title','created_at');
        if(!empty($data['title'])){
            $where['title'] = $data['title'];
        }else{
            $where['title'] = '';
        }

        if(!empty($data['created_at'])){
            $where['created_at'] = $data['created_at'];
        }else{
            $where['created_at'] = '';
        }
        $where['limit'] = $data['limit'];
        $where['page'] = $data['page'];

        $list = $noticeServices->getList($where);

        return  Helper::toJson('公告列表 ',200,$list);

    }


    //添加公告
    public function add(NoticeRequest $request){

        $all = $request->all();


        $userInfo = Helper::getPost('userinfo');

        $all['creator_uid']=$userInfo['user_info']['id'];
        $all['created_at'] = date('Y-m-d H:i:s');

        $data = $this->services->NociteSave($all);
        if($data){
            return Helper::toJson("创建成功", 200);
        }else{
            return Helper::toJson("创建失败", 400);
        }
    }


    //编辑公告
    public function update(NoticeRequest $request){
        $userInfo = Helper::getPost('userinfo');
        $all = $request->except(['id']);  //排除id
        $id = $request->only('id');  //只要ID
        $all['creator_uid']=$userInfo['user_info']['id'];
        $all['updated_at'] = date('Y-m-d H:i:s');

        $data = $this->services->NociteEdit($id,$all);

        if($data){
            return Helper::toJson("修改成功", 200);
        }else{
            return Helper::toJson("修改失败", 400);
        }

    }



    //删除公告
    public function del(Request $request){
        $userInfo = Helper::getPost('userinfo');
        $id = $request->only('id');
        $admin_uid=$userInfo['user_info']['id'];
        $data = $this->services->NociteDel($admin_uid,$id);
        if($data){
            return Helper::toJson("删除成功", 200);
        }else{
            return Helper::toJson("删除失败", 400);
        }

    }


    //公告详情
    public function info(Request $request){
        $id = $request->only('id');
        $data = $this->services->NociteInfo($id);
        $data['user_type'] = (string)$data['user_type'];
        $data['notice_type'] = (string)$data['notice_type'];

        return  Helper::toJson('公告详情 ',200,$data);
    }


    //平台列表
    public function un_list(Request $request,StoreServices $store){
        $list = $store->un_list();
        return  Helper::toJson('平台列表 ',200,$list);
    }



    //读取公告
    public function get_announcement(Request $request){

        $userInfo = Helper::getPost('userinfo');

        $userId = $userInfo['user_info']['id'];

        $data = $this->services->red_notice($userId);

        return  Helper::toJson('显示代理商公告 ',200,$data);
    }



    //已读公告
    public function read(Request $request){
        $userInfo = Helper::getPost('userinfo');

        $all['user_id'] = $userInfo['user_info']['id'];
        $all['notice_id'] = Helper::getPost('notice_id');

        $data = $this->services->read($all);

        if ($data){
            return Helper::toJson("公告已读", 200);
        }else{
            return Helper::toJson("读取失败", 400);
        }



    }



    //以看公告人数
    public function read_person(Request $request){

        $notice_id=Helper::getPost('notice_id');


        $data = $this->services->read_person($notice_id);


        return  Helper::toJson('一看公告人数 ',200,$data);
    }







}
